<%@page import="com.ym.b2b.cache.configcache.WebAppConfig"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="se" uri="http://zc.security/acl"%> 
<div class="easyui-layout" style="width:100%;height:100%;" fit="true">
	<div data-options="region:'north',title:'信息管理',split:true" style="height:70px;background-color:#e6edfd;">
    	<form action="" id="searchForm" name="searchForm">
    	<table class="screen">
			<tr>
				<td class="screen-n"><span>标题</span></td>
				<td class="screen-n">
					<input type="text" value="" id="s_newsTitle" class="screen-input"  onkeydown="pms.kd13('initList')" style="width:200px;">
				</td>
				<td class="screen-n"><span>作者</span></td>
				<td class="screen-n">
					<input type="text" id="s_newsWriter" class="screen-input"  onkeydown="pms.kd13('initList')">
				</td>
				<td class="screen-n"><span>来源</span></td>
				<td class="screen-n">
					<input type="text" id="s_newsSource" class="screen-input"  onkeydown="pms.kd13('initList')">
				</td>
				<td class="screen-n">
					<input type="button" class="button" value="查询" onclick="initList()">
					<input type="button" class="button" value="重置" onclick="pms.resetForm('searchForm');initList();" >
				    <input type="button" class="button" value="导出" onclick="loadExecl('initStoreOutCheckTableExcel')" >
				    
				    <input type="button" class="button" value="刷新前台缓存" onclick="refreshCacheAll()" >
				    
				</td>
			</tr>
		</table>
		</form>
    </div>
    
    
    <div id="toolbar"  >
    	<table>
		<tr>
			<se:hasPermissions actCode="ADD" >
			<td>
				<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="openAddWindow();">添加</a>
			</td>
			</se:hasPermissions>
			<se:hasPermissions actCode="MAINTAIN" >
			<td>
		        <a href="#" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'ext-icon-key',plain:true">批量发布</a>
		        <div id="mm1">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('newsStatus',1);">批量审核</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('newsStatus',0);">取消审核</div>
			    </div>
			    
			    <a href="#" class="easyui-menubutton" data-options="menu:'#mm2',iconCls:'ext-icon-key',plain:true">批量首页</a>
		        <div id="mm2">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('isIndex',1);">批量首页</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('isIndex',0);">取消首页</div>
			    </div>
			    
			    <a href="#" class="easyui-menubutton" data-options="menu:'#mm3',iconCls:'ext-icon-key',plain:true">批量频道</a>
		        <div id="mm3">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('isChannel',1);">批量频道</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('isChannel',0);">取消频道</div>
			    </div>
			    
			    <a href="#" class="easyui-menubutton" data-options="menu:'#mm4',iconCls:'ext-icon-key',plain:true">批量推荐</a>
		        <div id="mm4">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('isCommend',1);">批量推荐</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('isCommend',0);">取消推荐</div>
			    </div>
			    
			    <a href="#" class="easyui-menubutton" data-options="menu:'#mm5',iconCls:'ext-icon-key',plain:true">批量滚动</a>
		        <div id="mm5">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('isScroll',1);">批量滚动</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('isScroll',0);">取消滚动</div>
			    </div>
			    
			    <a href="#" class="easyui-menubutton" data-options="menu:'#mm6',iconCls:'ext-icon-key',plain:true">批量置顶</a>
		        <div id="mm6">
			    	<div data-options="iconCls:'icon-redo'" onclick="changenStatusBat('isTop',1);">批量置顶</div>
			        <div data-options="iconCls:'icon-undo'" onclick="changenStatusBat('isTop',0);">取消置顶</div>
			    </div>
			</td>
			</se:hasPermissions>
			<se:hasPermissions actCode="ADD" >
			<td>
				<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_delete',plain:true" onclick="deleteRecords();">删除</a>
			</td>
			</se:hasPermissions>
		</tr>
	</table>
    
    </div>
    
    <div data-options="region:'center',split:true" style="width: 100%;height: 100%">
		<table id="tableGrid"></table>
	</div>
</div>
<script>
var tbl;

function openAddWindow(){
	var params = {"typeId":<%=request.getParameter("typeId")%>}
	$.pmsDialog({
		id:"w_form",
		title:'新增',
		method:'post',
		isIf:'true',
		params:params,
		href:'<c:url value="/views/jsp/news/form_s.jsp"/>',
		fit:true,
	});
}

// 批量属性
function changenStatusBat(type,value){
	var rows = tbl.getSelectedData();
	if(rows.length>0){
		if(confirm("确定批量操作?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].newsId);
			}

			var newsIDList = ids.toString();
			
			var param = {};
			param[type] = value;
			param["newsIDList"] = newsIDList;
			
			$.pmsAjax({
				url:'<c:url value="/news/updateEmBNewsBat.do"/>',
				data:param,
				success:function(data){
					if(data.code==200){
						alertShow("批处理成功");
						initList();
					}else{
						alertError(data.resultMessage);
					}
				}
			}); 
		}
	}else{
		alertError('请选择操作记录');
	}
}
 
// 单个属性
function changenStatus(newsId,type,value){
	if(value!=1){
		value = 1;
	}else{
		value = 0;
	}
	
	var param = {};
	param[type] = value;
	param["newsId"] = newsId;
	
	$.pmsAjax({
		url:'<c:url value="/news/updateEmBNews.do"/>',
		data:param,
		success:function(data){
			if(data.code==200){
				initList();
			}else{
				alertError(data.resultMessage);
			}
		}
	}); 
}

function openUpdateWindow(newsId){
	var params = {"newsId":newsId,"typeId":<%=request.getParameter("typeId")%>}
	$.pmsDialog({
		id:"w_form",
		title:'修改',
		method:'post',
		params:params,
		isIf:'true',
		href:'<c:url value="/views/jsp/news/form_s.jsp"/>',
		width:1200,
		height:600,
		fit:true
		
	});
}

function deleteRecords(){
	var rows = tbl.getSelectedData();
	if(rows.length>0){
		if(confirm("确定批量删除?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].newsId);
			}
			var newsIdList = ids.toString();
			var param = {"newsIdList":newsIdList};
			$.pmsAjax({
				url:'<c:url value="/news/deleteMultipleEmBNews.do"/>',
				data:param,
				success:function(data){
					if(data.code==200){
						alertShow("删除批量成功");
						initList();
					}else{
						alertError(data.resultMessage);
					}
				}
			});
		}
	}else{
		alertError('请选择操作记录');
	}
	
}

function deleteRecord(newsId,i){
	if(confirm("确定删除记录?")){
		var param = {"newsId":newsId};
		$.pmsAjax({
			url:'<c:url value="/news/deleteEmBNews.do"/>',
			data:param,
			success:function(data){
				if(data.code==200){
					alertShow("删除记录成功");
					/*
					疑问：tbl.removeRow(i)；没有调用成功，目前只能整个页面刷新
					*/
					//tbl.removeRow(i);
					initList();
					
				}else{
					alertError(data.resultMessage);
				}
			}
		});
	}
}


$(function(){
	initList();
});
function getParams(){
	var queryParamss = {
			"newsTypeId":<%=request.getParameter("newsTypeId")%>,
			"newsTitle":$("#s_newsTitle").val(),
			"newsWriter":$('#s_newsWriter').val(),
			"newsSource":$('#s_newsSource').val()
	    };
    return queryParamss;
}
function initList(){
	tbl = $('#tableGrid').pDataGrid({
		 toolbar : '#toolbar',
		 columns:[[
		          {field:'ck',checkbox:true},
				  {field:'newsTitle',title:'信息标题',sortable:true,align:'center',
		        	  formatter : function(value, row) {
		        		  var str = '';
		        		  if(row.smallPic){
		        			  str = value +'<span style="color:blue;">[图]<span>';
		        		  }else{
		        			  str = value;
		        		  }
		        		  return str;
		        	  }
				  },
				  {field:'newsTypeName',title:'信息类别',width:'60',sortable:true},
				  {field:'newsWriter',title:'作者',width:'60',sortable:true},
				  {field:'newsSource',title:'来源',width:'60',sortable:true},
				  {field:'publishDate',title:'发布时间',width:'100',sortable:true,align:'center'},
				  {field:'endDate',title:'结束时间',width:'100',sortable:true,align:'center'},
				  {field:'newsStatus',title:'发布',width:'50',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '未发布';
								  break;
							  case 1:
								  str = '已发布';
								  break;
							  default:
								  str = '未发布';
								  break;
						  }
						  return str;
					  }
				  },
				  {field:'isIndex',title:'首页',width:'40',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '';
								  break;
							  case 1:
								  str = '首页';
								  break;
							  default:
								  str = '';
								  break;
						  }
						  return str;
					  }
				  },
				  {field:'isChannel',title:'频道',width:'40',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '';
								  break;
							  case 1:
								  str = '频道';
								  break;
							  default:
								  str = '';
								  break;
						  }
						  return str;
					  }
				  },
				  {field:'isCommend',title:'推荐',width:'40',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '';
								  break;
							  case 1:
								  str = '推荐';
								  break;
							  default:
								  str = '';
								  break;
						  }
						  return str;
					  }
				  },
				  {field:'isScroll',title:'滚动',width:'40',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '';
								  break;
							  case 1:
								  str = '滚动';
								  break;
							  default:
								  str = '';
								  break;
						  }
						  return str;
					  }
				  },
				  {field:'isTop',title:'置顶',width:'40',sortable:true,align:'center',
					  formatter : function(value, row) {
						  var str = '';
						  switch(value){
							  case 0:
								  str = '';
								  break;
							  case 1:
								  str = '置顶';
								  break;
							  default:
								  str = '';
								  break;
						  }
						  return str;
					  }
				  },
				  {
						field : 'action',
						title : '操作',
						width : '180',
						align : 'center',
						formatter : function(value, row, index) {
							var str = '';
							<se:hasPermissions actCode="UPDATE">
								str += '<img class="iconImg ext-icon-note_edit" title="编辑" onclick="openUpdateWindow('+row.newsId+');"/>';
							</se:hasPermissions>
							<se:hasPermissions actCode="DELETE">
								str += '<img class="iconImg ext-icon-note_delete" title="删除" onclick="deleteRecord('+row.newsId+',' + index + ');"/>';
							</se:hasPermissions>	
							<se:hasPermissions actCode="APPROVE">
								str += '<img class="iconImg ext-icon-key" title="审核" onclick="changenStatus('+row.newsId+',\'newsStatus\','+row.newsStatus+');"/>';
							</se:hasPermissions>
							<se:hasPermissions actCode="MAINTAIN">	
								str += '<img class="iconImg ext-icon-text_align_center" title="首页" onclick="changenStatus('+row.newsId+',\'isIndex\','+row.isIndex+');"/>';
								
								str += '<img class="iconImg ext-icon-text_columns" title="频道" onclick="changenStatus('+row.newsId+',\'isChannel\','+row.isChannel+');"/>';
								
								str += '<img class="iconImg ext-icon-thumb_up" title="推荐" onclick="changenStatus('+row.newsId+',\'isCommend\','+row.isCommend+');"/>';
								
								str += '<img class="iconImg ext-icon-sport_8ball" title="滚动" onclick="changenStatus('+row.newsId+',\'isScroll\','+row.isScroll+');"/>';
								
								str += '<img class="iconImg ext-icon-shape_align_top" title="置顶" onclick="changenStatus('+row.newsId+',\'isTop\','+row.isTop+');"/>';
							</se:hasPermissions>
							return str;
						}
					} 
				  ]
			  ],
		    url:'<c:url value="/news/searchPageNews.do"/>',  
		    pagination:true,
		    pageSize:30,
		    fitColumns:false,
		    singleSelect:false,
		    queryParams:getParams(),
		    completeCallback:function(data){
				if(data.resultCode == "100"){
					alertError(data.errorMsg);
				} 
			},
			onDblClickRow:function(index,row){
				openUpdateWindow(row.newsId);
			},
			onLoadSuccess : function(data) {
				var panel = $(this).datagrid('getPanel');
				var tr = panel.find('div.datagrid-body tr');
				tr.each(function() {
					var td = $(this).children('td[field="newsTitle"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
			}
	}); 
	
}
//导出
function initStoreOutCheckTableExcel(){
	loadExcelCallBack('<c:url value="/news/searchPageNewsExcel.do"/>',getParams());
} 
function refreshCacheAll(){
	$.pmsDialog({
		id:"refresh_cache",
		title:'刷新资讯类缓存',
		method:'post',
		params:{"cacheName":"news"},
		isIf:'true',
		href:'<%=WebAppConfig.getConfigMap().get("WEB_URL") %>/cache/refreshCacheAll.s',
		width: 400,
		height:200,
		 
	});
}
</script>
